package com.zlp.netty.lecture;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

/**
 * @author: zhulaiping
 * @time: 2022/9/6 15:44
 */
@Slf4j
public class PackageClient {

    public static void main(String[] args) throws InterruptedException {
        //for (int i=0;i<10;i++)
            send();
    }

    private static void send() throws InterruptedException {
        NioEventLoopGroup worker = new NioEventLoopGroup();
        Channel channel = new Bootstrap()
                .group(worker)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        //用于打印客户端发送的数据
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            //会在连接 channel 建立后，会触发active事件
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                log.debug("sending...");
                                // 每次发送16个字节的数据，共发送10次
                                /*for (int i=0;i<10;i++) {
                                    ByteBuf buffer = ctx.alloc().buffer();
                                    buffer.writeBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
                                    ctx.writeAndFlush(buffer);
                                    //ctx.channel().close();
                                }*/
                                //定长消息发送
                                /*ByteBuf buffer = ctx.alloc().buffer();
                                char ch = 'a';
                                for (int i=0;i<5;i++){
                                    StringBuilder sb = new StringBuilder();
                                    for (int j=0;j<new Random().nextInt(10)+1;j++){
                                        sb.append(ch);
                                    }
                                    int len = sb.length();
                                    for (int j=0;j<10-len;j++){
                                        sb.append("_");
                                    }

                                    buffer.writeBytes(sb.toString().getBytes());
                                    ch++;
                                }
                                ctx.writeAndFlush(buffer);*/
                                //行消息发送
                                ByteBuf buffer = ctx.alloc().buffer();
                                char ch = 'a';
                                for (int i=0;i<10;i++){
                                    StringBuilder sb = new StringBuilder();
                                    for (int j=0;j<new Random().nextInt(10)+1;j++){
                                        sb.append(ch);
                                    }
                                    sb.append("\n");
                                    buffer.writeBytes(sb.toString().getBytes());
                                    ch++;
                                }
                                ctx.writeAndFlush(buffer);
                            }
                        });
                    }
                }).connect("localhost", 8080)
                .sync()
                .channel();

        channel.closeFuture();
    }
}
